package com.loukou.lkt.lkt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loukou.lkt.lkt.constant.Urls;
import com.loukou.lkt.lkt.dao.SaleOrderDao;
import com.loukou.lkt.lkt.dao.SaleOrderDetailDao;
import com.loukou.lkt.lkt.dao.SaleOrderExtmDao;
import com.loukou.lkt.lkt.dao.SaleOrderGoodsRDao;
import com.loukou.lkt.lkt.dao.SaleOrderReturnDao;
import com.loukou.lkt.lkt.dao.WarehouseDao;
import com.loukou.lkt.lkt.entity.SaleOrder;
import com.loukou.lkt.lkt.entity.SaleOrderDetail;
import com.loukou.lkt.lkt.entity.SaleOrderExtm;
import com.loukou.lkt.lkt.entity.SaleOrderGoodsR;
import com.loukou.lkt.lkt.entity.SaleOrderReturn;
import com.loukou.lkt.lkt.entity.Warehouse;
import com.loukou.lkt.lkt.enums.StockInOrderTypeEnum;
import com.loukou.lkt.lkt.enums.StockOutOrderTypeEnum;
import com.loukou.lkt.lkt.enums.VoucherPrefixEnum;
import com.loukou.lkt.lkt.req.SaleOrderDetailReq;
import com.loukou.lkt.lkt.req.SaleOrderReq;
import com.loukou.lkt.lkt.req.SaleOrderReturnDto;
import com.loukou.lkt.lkt.req.SaleOrderReturnGoodsRDto;
import com.loukou.lkt.lkt.req.WmsInOrderNoticeDetailReq;
import com.loukou.lkt.lkt.req.WmsInOrderNoticeReq;
import com.loukou.lkt.lkt.req.WmsOutOrderNoticeDetailReq;
import com.loukou.lkt.lkt.req.WmsOutOrderNoticeReq;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.resp.WmsNoticeRespDto;
import com.loukou.lkt.lkt.service.SaleOrderService;
import com.loukou.lkt.lkt.service.SerialNumberService;
import com.loukou.lkt.lkt.utils.PostUtils;

@Service
public class SaleOrderServiceImpl implements SaleOrderService{
	
	@Autowired
	private SaleOrderDao saleOrderDao;
	
	@Autowired
	private SaleOrderDetailDao saleOrderDetailDao;
	
	@Autowired
	private SaleOrderExtmDao saleOrderExtmDao;
	
	@Autowired
	private WarehouseDao warehouseDao;
	
	@Autowired
	private SaleOrderReturnDao saleOrderReturnDao;
	
	@Autowired
	private SaleOrderGoodsRDao saleOrderGoodsRDao;
	
	@Autowired
	private SerialNumberService serialNumberService;
	
	
	@Override
	public RespDto<String> receiveSaleOrder(SaleOrderReq req) {
		RespDto<String> resp = new RespDto<String>();
		if(req == null || req.getExtm() == null || req.getDetailList()==null || req.getDetailList().size()==0){
			resp.setCode(401);
			resp.setResult("参数错误");
			return resp;
		}
		SaleOrder saleOrder = new SaleOrder();
		BeanUtils.copyProperties(req, saleOrder);
		saleOrderDao.save(saleOrder);
		SaleOrderExtm extm = new SaleOrderExtm();
		BeanUtils.copyProperties(req.getExtm(), extm);
		extm.setOrderId(saleOrder.getId());
		saleOrderExtmDao.save(extm);
		List<SaleOrderDetailReq> detailReqList = req.getDetailList();
		List<SaleOrderDetail> saleOrderDetailList = new ArrayList<SaleOrderDetail>();
		for(SaleOrderDetailReq detailReq : detailReqList){
			SaleOrderDetail detail = new SaleOrderDetail();
			BeanUtils.copyProperties(detailReq, detail);
			detail.setOrderId(saleOrder.getId());
			saleOrderDetailDao.save(detail);
			saleOrderDetailList.add(detail);
		}
		
		//给WMS生成出库单
		WmsOutOrderNoticeReq wmsOutOrderNotice = new WmsOutOrderNoticeReq();//LKT根据调拨单通知WMS出库
		wmsOutOrderNotice.setOutbizCode(""+saleOrder.getOrderCode());
		Warehouse warehouse = warehouseDao.findOne(saleOrder.getMiniWarehouseId());
		if(warehouse != null){
			wmsOutOrderNotice.setWarehouseNo(warehouse.getWarehouseCode());
		}
		wmsOutOrderNotice.setOrderType(StockOutOrderTypeEnum.TYPE_SALE.getType());
		wmsOutOrderNotice.setRemark(saleOrder.getRemark());
		List<WmsOutOrderNoticeDetailReq> wmsOutOrderDetailList = new ArrayList<WmsOutOrderNoticeDetailReq>();
		for(SaleOrderDetail detail : saleOrderDetailList){
			WmsOutOrderNoticeDetailReq wmsOutOrderDetail = new WmsOutOrderNoticeDetailReq();
			wmsOutOrderDetail.setOutbizId(""+detail.getId());
			wmsOutOrderDetail.setCustomerGoodsId(""+detail.getGoodsId());
			wmsOutOrderDetail.setGoodsName(detail.getGoodsName());
			wmsOutOrderDetail.setPrice(detail.getTaxPrice());
			wmsOutOrderDetail.setQuantity(detail.getDeliveryNumber());
			wmsOutOrderDetail.setAmount(detail.getTaxAmount());
			wmsOutOrderDetailList.add(wmsOutOrderDetail);
		}
		wmsOutOrderNotice.setDetails(wmsOutOrderDetailList);
		Object wmsOutOrder = PostUtils.postData(Urls.LktTellWmsOutOrderNotice, wmsOutOrderNotice);
		if(wmsOutOrder == null){
			resp.setCode(405);
			resp.setResult("通知WMS出库失败");
			return resp;
		}
		WmsNoticeRespDto wmsResp = (WmsNoticeRespDto)wmsOutOrder;
		if(!"200".equals(wmsResp.getCode())){
			resp.setCode(Integer.valueOf(wmsResp.getCode()));
			resp.setResult(wmsResp.getSuccess());
			resp.setMsg(wmsResp.getErrorMsg());
			return resp;
		}
		resp.setCode(200);
		resp.setResult("成功接收");
		return resp;
	}


	@Override
	public RespDto<String> receiveSaleOrderReturn(SaleOrderReturnDto req) {
		RespDto<String> resp = new RespDto<String>();
		if(req == null || req.getGoodsRList() == null || req.getGoodsRList().size() == 0){
			resp.setCode(401);
			resp.setResult("参数错误");
			return resp;
		}
		String saleOrderCode = req.getSaleOrderCode();
		SaleOrder saleOrder = saleOrderDao.findByOrderCode(saleOrderCode);
		if(saleOrder == null){
			resp.setCode(500);
			resp.setResult("订单编号未曾出售过");
			return resp;
		}
		int warehouseId = saleOrder.getWarehouseId();
		Warehouse warehouse = warehouseDao.findOne(warehouseId);
		if(warehouse == null){
			resp.setCode(501);
			resp.setResult(warehouseId + "仓库不存在");
			return resp;
		}
		SaleOrderReturn saleOrderReturn = new SaleOrderReturn();
		BeanUtils.copyProperties(req, saleOrderReturn);
		saleOrderReturn.setOrderId(saleOrder.getId());
		String orderReturnCode = serialNumberService.generateNewVoucherCode(VoucherPrefixEnum.PREFIX_RETURN_ORDER, saleOrder.getWarehouseId());
		saleOrderReturn.setOrderCode(orderReturnCode);
		List<SaleOrderGoodsR> saleOrderGoodsRList = new ArrayList<SaleOrderGoodsR>();
		saleOrderReturnDao.save(saleOrderReturn);
		List<SaleOrderReturnGoodsRDto> goodsReqList = req.getGoodsRList();
		for(SaleOrderReturnGoodsRDto goodsReq:  goodsReqList){
			SaleOrderGoodsR goodsR = new SaleOrderGoodsR();
			BeanUtils.copyProperties(goodsReq, goodsR);
			goodsR.setOrderReturnId(saleOrderReturn.getId());
			saleOrderGoodsRList.add(goodsR);
		}
		
		//给WMS生成入库单
		WmsInOrderNoticeReq wmsInOrderNoticeReq = new WmsInOrderNoticeReq();
		wmsInOrderNoticeReq.setOutbizCode(saleOrderReturn.getOrderCode());
		wmsInOrderNoticeReq.setWarehouseNo(warehouse.getWarehouseCode());
		wmsInOrderNoticeReq.setOrderType(StockInOrderTypeEnum.TYPE_RETURN.getType());
		wmsInOrderNoticeReq.setRemark(saleOrderReturn.getRemark());
		List<WmsInOrderNoticeDetailReq> detailReqList = new ArrayList<WmsInOrderNoticeDetailReq>();
		for(SaleOrderGoodsR goodsR: saleOrderGoodsRList){
			WmsInOrderNoticeDetailReq detailReq = new WmsInOrderNoticeDetailReq();
			detailReq.setOutbizId(""+goodsR.getId());
			detailReq.setCustomerGoodsId(""+goodsR.getSpecId());
			detailReq.setGoodsName(goodsR.getGoodsName());
			detailReq.setPrice(goodsR.getPrice());
			detailReq.setQuantity(goodsR.getGoodsNum());
			detailReq.setAmount(goodsR.getGoodsAmount());
			detailReqList.add(detailReq);
		}
		wmsInOrderNoticeReq.setDetails(detailReqList);
		Object wmsInOrder = PostUtils.postData(Urls.LktTellWmsInOrderNotice, wmsInOrderNoticeReq);
		if(wmsInOrder == null){
			resp.setCode(405);
			resp.setResult("通知WMS入库失败");
			return resp;
		}
		WmsNoticeRespDto wmsResp = (WmsNoticeRespDto)wmsInOrder;
		if(!"200".equals(wmsResp.getCode())){
			resp.setCode(Integer.valueOf(wmsResp.getCode()));
			resp.setResult(wmsResp.getSuccess());
			resp.setMsg(wmsResp.getErrorMsg());
			return resp;
		}
		resp.setCode(200);
		resp.setResult("成功接收");
		return resp;
	}


	@Override
	public RespDto<String> wmsRealStockInOut(WmsInOrderNoticeReq req) {
		RespDto<String> resp = new RespDto<String>();
		if(req == null || req.getDetails() == null || req.getDetails().size() == 0){
			resp.setCode(501);
			resp.setResult("参数错误");
		}
		String orderCode = req.getOutbizCode();
		List<WmsInOrderNoticeDetailReq> detailReqList = req.getDetails();
		for(WmsInOrderNoticeDetailReq detailReq: detailReqList){
			SaleOrderGoodsR goodsR = saleOrderGoodsRDao.findOne(Integer.valueOf(detailReq.getOutbizId()));
			
			//咦，退货怎么没有退货实际
			
		}
		
		
		return null;
	}
}
